package com.jinly.ryder.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinly.ryder.manage.service.model.InstanceType;
import com.jinly.ryder.monitor.dao.MonitorDataMapper;
import com.jinly.ryder.monitor.dao.entity.MonitorDataDO;
import com.jinly.ryder.monitor.service.MonitorDataQueryService;
import com.jinly.ryder.monitor.service.mapping.MonitorDataMapping;
import com.jinly.ryder.monitor.service.model.MonitorData;
import com.jinly.ryder.monitor.service.model.MonitorKeyType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class MonitorDataQueryServiceImpl implements MonitorDataQueryService {

    private final MonitorDataMapper monitorDataMapper;
    private final MonitorDataMapping monitorDataMapping;

    @Override
    public List<MonitorData> queryData(Set<Integer> instanceIds, Set<MonitorKeyType> keyTypes, LocalDateTime begin, LocalDateTime end) {
        Wrapper<MonitorDataDO> queryWrapper = new QueryWrapper<MonitorDataDO>().lambda()
                .in(MonitorDataDO::getInstanceId, instanceIds)
                .eq(MonitorDataDO::getInstanceType, InstanceType.APP)
                .in(MonitorDataDO::getKeyType, keyTypes)
                .between(MonitorDataDO::getTime, begin.toInstant(ZoneOffset.ofHours(8)).toEpochMilli(), end.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
        return monitorDataMapping.toModelList(monitorDataMapper.selectList(queryWrapper));
    }
}
